import matplotlib.pyplot as plt
import numpy as np
import time

from my_db_ops import connectDb, closeDb, getDbStockData

if __name__ == "__main__":
    #解决中文显示问题
    plt.rcParams['font.sans-serif'] = ['SimHei'] # 指定默认字体
    plt.rcParams['axes.unicode_minus'] = False # 解决保存图像是负号'-'显示为方块的问题

    dbName = "/home/colby/sd_480/01.liang_hua_tou_zi/01.python_gu_piao_liang_hua/00.huo_qu_gu_piao_shu_ju/stock-data.db"
    conn = connectDb(dbName)
    stock1 = getDbStockData(conn, stockCode='sh.000001')
    stock2 = getDbStockData(conn, stockCode='sh.000002')
    stock3 = getDbStockData(conn, stockCode='sh.000003')
    
    fig = plt.figure(figsize=(14, 5), dpi=100, facecolor="white") # 创建 fig 对象
    graph_sma = fig.add_subplot(1, 1, 1) # 创建子图
    
    graph_sma.plot(np.arange(0, len(stock1.index)), stock1['Low'], 'red', label='sh.000001-Low')
    graph_sma.plot(np.arange(0, len(stock2.index)), stock2['Low'], 'green', label='sh.000002-Low')
    graph_sma.plot(np.arange(0, len(stock3.index)), stock3['Low'], 'blue', label='sh.000003-Low')

    graph_sma.legend(loc='best')
    graph_sma.set_title(u'股票曲线')
    graph_sma.set_ylabel(u'价钱')

    graph_sma.set_xlim(0, len(stock1.Date)) # 设置 x 轴的范围
    graph_sma.set_xticks(range(0, len(stock1.Date), 15)) # x 轴 刻度设定每15天标一个日期
    graph_sma.set_xticklabels( [stock1.Date[index] \
        for index in graph_sma.get_xticks()]) # 标签设置日期

    fig.autofmt_xdate(rotation=45)
    plt.show()